package com.dhc.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.EverythingMatcher;
import org.quartz.impl.matchers.KeyMatcher;


public class JobListener implements org.quartz.Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("执行了");

    }

    public static void main(String[] args) throws Exception{
        //Scheduler调度器 工厂中获取调度实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //Job任务实例
        JobDetail jobDetail = JobBuilder.newJob(JobListener.class)
                .withIdentity("job1", "jobGroup1")//1任务名2组名
                .build();
//        jobDetail.getKey().getName();//任务名
//        jobDetail.getKey().getGroup();//组名(未设置显示DEFAULT)
//        System.out.println(jobDetail.getJobClass().getName());//com.dhc.quartz.Job
        //Trigger触发器
        Trigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "triggerGroup1")
                .startNow()//马上启动触发器
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
                .build();
        //让调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        //创建调度器的监听SchedulerListener
        scheduler.getListenerManager().addSchedulerListener(new SchedulerListenerActive());
        //启动
        scheduler.start();

        //全局JobListener
//        scheduler.getListenerManager().addJobListener(new JobListenerActive(),EverythingMatcher.allJobs());
        //局部JobListener
//        scheduler.getListenerManager().addJobListener(new JobListenerActive(),KeyMatcher.keyEquals(JobKey.jobKey("job1","jobGroup1")));

        //全局TriggerListener
//        scheduler.getListenerManager().addTriggerListener(new TriggerListenerActive(),EverythingMatcher.allTriggers());
        //局部TriggerListener
//        scheduler.getListenerManager().addTriggerListener(new TriggerListenerActive(),KeyMatcher.keyEquals(TriggerKey.triggerKey("trigger1","triggerGroup1")));

    }
}
